#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

#产品线名称:packages-w1140 必须跟主线一致 http://192.168.0.123/repositories/AppsOfMtk/product/PD1110/packages-w1140/
#
moudules_file=/tmp/modules
moudules_url=/tmp/modules_url
products_url=/tmp/products_url
svn_error=/tmp/svn.error
config_file=$1
authz_file=$2
tags_list=`parseini.shc $config_file get reposinfo tags | tr ',' ' '`
packages_list=`parseini.shc $config_file get reposinfo packages | tr ',' ' '`
producttype_list=`parseini.shc $config_file get reposinfo producttype|tr ',' ' '`
domain=`parseini.shc $config_file get reposinfo domain`

if test ! -f  $config_file; then
  echoTextRed "$0  config_file  auth_file"
  exit
fi

#获取仓库最新的一些信息
rm -rf ${moudules_file}*
rm -rf $svn_error

##以下代码生成权限文件
echoTextBlue "权限文件 $authz_file 生成中..... "
if test ! -f $authz_file; then
  touch $authz_file
else
  result=`question "权限文件 $authz_file 已经存在,是否覆盖?" 1 yes`
  if [ "$result" = "yes" ]; then
    mv $authz_file ${authz_file}_`sh_get_date_and_time2`
    touch $authz_file
  else
    exit
  fi
fi

echoTextBlue "生成默认组信息......"
date
start_time=`sh_get_date_and_time2`
groups_list=`cat $config_file |grep ^def_g_ |awk -F"=" '{print $1}'`
echo "[groups]" > $authz_file 
#groups_list=`parseini.shc $config_file get groups groups|tr ',' ' '`
for group in $groups_list
do
	user_list=`cat $config_file |grep ^$group|awk -F"=" '{print $2}'`
	echo "$group=$user_list"   >> $authz_file 
done
#echo ""  >> $authz_file 

echoTextBlue "通过模块生成组信息......"
date

modules_list=`cat $config_file |grep "\["|grep -v -E "groups|reposinfo"|sort | uniq |tr '[' ' '|tr ']' ' ' |tr '\n' ' '`
#    echo "modules_list=$modules_list"
#去掉重复项wallpapers|gles20_Basic等
modules_list="`echo $modules_list |sed "s/ /\\n/g" | sort | uniq |tr '\n' ' '`"
usersofpermit_list="usersofpermit_r usersofpermit_w"
for module in $modules_list
do
  for usersofpermit in $usersofpermit_list
  do
  result=`parseini.shc $config_file  get $module $usersofpermit`

  if [ -n "$result" ]; then
      if [ "$usersofpermit" = "usersofpermit_r" ];then
	 module_name="g_${module}_r"
      else
	 module_name="g_${module}_w"
      fi
      #usersofpermit=`echo $result|tr ',' ' '`
      echo "$module_name=$result" >>$authz_file
  fi
  done
done
echo "">>$authz_file

echoTextBlue "文档仓权限生成......"
date
repos_list=`parseini.shc $config_file get reposinfo repos_docs | tr ',' ' '`
for repos in $repos_list
do
   echo "[$repos:/]"  >>$authz_file
   echo "@def_g_DocsAndTools_read = r" >>$authz_file
   echo "@def_g_DocsAndTools_md_eg = r" >>$authz_file
   echo "@def_g_DocsAndTools_sqa = r" >>$authz_file
   echo "@def_g_softdev_depart2 = rw"  >>$authz_file
   echo "@def_g_DocsAndTools_manage = rw"  >>$authz_file
   echo "@def_g_DocsAndTools_admin = rw"  >>$authz_file
   echo "@def_g_DocsAndTools_md_leader = rw"  >>$authz_file
   echo "">>$authz_file
   echo "[DocsAndTools:/scripts]"  >>$authz_file
   echo "@def_g_DocsAndTools_read = " >>$authz_file
   echo "@def_g_DocsAndTools_md_eg = " >>$authz_file
   echo "@def_g_DocsAndTools_sqa = " >>$authz_file
   echo "@def_g_softdev_depart2 = "  >>$authz_file
   echo "@def_g_DocsAndTools_manage = "  >>$authz_file
   echo "@def_g_DocsAndTools_md_leader = "  >>$authz_file

   modules_list=`cat $config_file |grep  "\["|grep -v -E "groups|reposinfo|product|project"|sort | uniq |tr '[' ' '|tr ']' ' ' |tr '\n' ' '`
   for module in $modules_list
   do
   
      echo "[$repos:/$module]"|tr '|' '/'  >>$authz_file
      result_r=`grep ${module}_r $authz_file`
      result_w=`grep ${module}_w $authz_file`
      if [ "$result_r" != "" ];then
         echo "@g_${module}_r = r"   >>$authz_file
      fi
      if [ "$result_w" != "" ];then
         echo "@g_${module}_w = rw"   >>$authz_file
      fi
   done
   modules_list=`cat $config_file |grep  "\["|grep "product"|sort | uniq |tr '[' ' '|tr ']' ' ' |tr '\n' ' '`
   projectline_list=`cat $config_file |grep  "\["|grep "project"|sort | uniq |tr '[' ' '|tr ']' ' ' |tr '\n' ' '`
   for producttype in $producttype_list
   do
      for module in $modules_list
      do
         echo "[$repos:/Docs/$module]" |sed "s/product/$producttype/"|tr '|' '/'  >>$authz_file
	 result_r=`grep ${module}_r $authz_file`
         result_w=`grep ${module}_w $authz_file`
         if [ "$result_r" != "" ];then
            echo "@g_${module}_r = r"   >>$authz_file
         fi
         if [ "$result_w" != "" ];then
            echo "@g_${module}_w = rw"   >>$authz_file
         fi
      done
      echo "[$repos:/Docs/$producttype/MtkPatchRecord]" >>$authz_file
      echo "* = rw"  >>$authz_file
      project_list=`svn.shc ls http://$domain/repositories/$repos/Docs/$producttype 2> /dev/null |grep -E "PD|TD|MT|QCOM"|tr '/' ' '`

      for project in $project_list
      do
			has_doc=`svn.shc info http://$domain/repositories/$repos/Docs/$producttype/$project/docs 2> /dev/null |grep URL`
			if [ "$has_doc" != "" ];then
				project="$project/docs"
			fi

			for projectline in $projectline_list
			do

				#if [ "$has_doc" != "" ];then
				#	echo "[$repos:/Docs/$producttype/$projectline]" |sed "s/project/$project\/docs/" |tr '|' '/'  >>$authz_file
				#else
				echo "[$repos:/Docs/$producttype/$project/$projectline]" |sed "s/\/project//" |tr '|' '/'  >>$authz_file
				#echo "$repos:/Docs/$producttype/$project/$projectline"
				#fi
				result_r=`grep ${projectline}_r $authz_file`
				result_w=`grep ${projectline}_w $authz_file`
				if [ "$result_r" != "" ];then
					echo "@g_${projectline}_r = r"   >>$authz_file
				fi
				if [ "$result_w" != "" ];then
					echo "@g_${projectline}_w = rw"   >>$authz_file
				fi
			done
      done
      echo "" >>$authz_file
   done


done

echo "">>$authz_file
echoTextBlue "权限文件$authz_file已生成完毕！"
date
end_time=`sh_get_date_and_time2`
((sun_time1=${end_time}-${start_time}))
echoTextBlue "耗时: $sun_time1 秒!"


chmod 777 $authz_file
replace=`question "是否覆盖权限文件authz-docsandtools?"  2 "yes"`
if [ "$replace" = "yes" ];then
	mv $authz_file  authz-docsandtools
fi
